<?php

namespace app\award;

use app\models\order\Order;
use app\models\order\OrderItem;
use app\models\product\ProductArea;
use app\models\SetParam;
use app\models\SetUserUp;
use app\models\user\User;
use app\models\user\UserLevel;
use app\services\Chain;

class Up2
{
    protected $order;
    protected $user;

    public function __construct(Order $order)
    {
        $this->order = $order;
        $this->user = User::findOne(['id' => $this->order->user_id]);

        $first_order_item = OrderItem::find()
            ->where(['order_id' => $order->id])
            ->orderBy(['id' => SORT_ASC])
            ->limit(1)
            ->one();
        $product_id = $first_order_item->product_id;

        $this->set = SetUserUp::getLastOne();

        $is_bao = ProductArea::checkBao($product_id);
        $is_new = ProductArea::checkNew1($product_id);

        // 必须是报单区
        if ($is_bao) {
            $this->_bao();
        }

        // 新报单区
        if ($is_new) {
            $this->_new();
        }
    }

    // 报单
    protected function _bao()
    {
        // 升级本人
        $amount = $this->order->total_pay - $this->order->total_freight;
        if ($amount >= $this->set->vip1_buy) {
            if ($this->user->level < UserLevel::$keys['vip1']) {
                $this->user->level = UserLevel::$keys['vip1'];
                $this->user->save();
            }
        }

        // 升级推荐链
        $chain = Chain::getReferrerChain($this->user->id, [
            UserLevel::$keys['vip1'],
            UserLevel::$keys['vip2'],
        ], true);
        if ($chain) {
            foreach ($chain as $user) {
                if ($user->level >= UserLevel::$keys['vip2']) {
                    continue;
                }
                $count = (int) User::find()
                    ->where(['referrer_id' => $user->id])
                    ->andWhere(['level' => [
                        UserLevel::$keys['vip1'],
                        UserLevel::$keys['vip2'],
                        // 新加上的
                        UserLevel::$keys['vip3'],
                    ]])
                    ->count();
                if ($count >= $this->set->vip2_num) {
                    $user->level = UserLevel::$keys['vip2'];
                    $user->save();
                }
            }
        }
    }

    // 新报单
    // protected function _new()
    // {
    //     $amount = $this->order->total_pay - $this->order->total_freight;
    //
    //     // 升级本人
    //     if ($amount >= $this->set->s2_vip23_buy) {
    //         if ($this->user->new_level < UserLevel::$keys['vip23']) {
    //             $this->user->new_level = UserLevel::$keys['vip23'];
    //             $this->user->save();
    //         }
    //     }
    //     else if ($amount >= $this->set->s2_vip22_buy) {
    //         if ($this->user->new_level < UserLevel::$keys['vip22']) {
    //             $this->user->new_level = UserLevel::$keys['vip22'];
    //             $this->user->save();
    //         }
    //     }
    //     else if ($amount >= $this->set->s2_vip21_buy) {
    //         if ($this->user->new_level < UserLevel::$keys['vip21']) {
    //             $this->user->new_level = UserLevel::$keys['vip21'];
    //             $this->user->save();
    //         }
    //     }
    //
    //     // 升级推荐链
    //     $chain = Chain::getReferrerChainNew($this->user->id, [
    //         UserLevel::$keys['vip21'],
    //         UserLevel::$keys['vip22'],
    //         UserLevel::$keys['vip23'],
    //         UserLevel::$keys['vip24'],
    //     ], true);
    //     if ($chain) {
    //         foreach ($chain as $user) {
    //             if ($user->new_level >= UserLevel::$keys['vip24']) {
    //                 continue;
    //             }
    //             $count = (int) User::find()
    //                 ->where(['referrer_id' => $user->id])
    //                 ->andWhere(['new_level' => [
    //                     UserLevel::$keys['vip21'],
    //                     UserLevel::$keys['vip22'],
    //                     UserLevel::$keys['vip23'],
    //                     UserLevel::$keys['vip24'],
    //                 ]])
    //                 ->count();
    //             if ($count >= $this->set->s2_vip24_num) {
    //                 $user->new_level = UserLevel::$keys['vip24'];
    //                 $user->save();
    //             }
    //         }
    //     }
    // }
    protected function _new()
    {
        $amount = $this->order->total_pay - $this->order->total_freight;

        // 升级本人
        // if ($amount >= $this->set->s2_vip23_buy) {
        //     if ($this->user->new_level < UserLevel::$keys['vip23']) {
        //         $this->user->new_level = UserLevel::$keys['vip23'];
        //         $this->user->save();
        //     }
        // }
        if ($amount >= $this->set->s2_vip22_buy) {
            if ($this->user->new_level < UserLevel::$keys['vip22']) {
                $this->user->new_level = UserLevel::$keys['vip22'];
                $this->user->save();
            }
        }
        else if ($amount >= $this->set->s2_vip21_buy) {
            if ($this->user->new_level < UserLevel::$keys['vip21']) {
                $this->user->new_level = UserLevel::$keys['vip21'];
                $this->user->save();
            }
        }

        // 升级推荐链
        // $chain = Chain::getReferrerChainNew($this->user->id, [
        //     UserLevel::$keys['vip21'],
        //     UserLevel::$keys['vip22'],
        //     UserLevel::$keys['vip23'],
        //     UserLevel::$keys['vip24'],
        // ], true);
        // if ($chain) {
        //     foreach ($chain as $user) {
        //         if ($user->new_level >= UserLevel::$keys['vip24']) {
        //             continue;
        //         }
        //         $count = (int) User::find()
        //             ->where(['referrer_id' => $user->id])
        //             ->andWhere(['new_level' => [
        //                 UserLevel::$keys['vip21'],
        //                 UserLevel::$keys['vip22'],
        //                 UserLevel::$keys['vip23'],
        //                 UserLevel::$keys['vip24'],
        //             ]])
        //             ->count();
        //         if ($count >= $this->set->s2_vip24_num) {
        //             $user->new_level = UserLevel::$keys['vip24'];
        //             $user->save();
        //         }
        //     }
        // }
    }
}